/*************************************************************
*	Converts the measurements in one format (Fahrenheit) to another
*	Also, data that is not written to files are not passed on, truly "filtering" data
*************************************************************/

public class ConverterFilter extends InstrumentationFilterFramework{
	
	public void run()
    {

		int MeasurementLength = 8;		// This is the length of all measurements (including time) in bytes
		int IdLength = 4;				// This is the length of IDs in the byte stream

		byte databyte = 0;				// This is the data byte read from the stream
		int bytesread = 0;				// This is the number of bytes read from the stream

		long measurement;				// This is the word used to store all measurements - conversions are illustrated.
		int id;							// This is the measurement id
		int i;							// This is a loop counter

		/*************************************************************
		*	First we announce to the world that we are alive...
		**************************************************************/

		System.out.print( "\n" + this.getName() + "::Converter Reading ");

		while (true)
		{
			try
			{
				/*************************************************************
				 *	Read id
				 **************************************************************/

				id = 0;

				for (i=0; i<IdLength; i++ )
				{
					databyte = ReadFilterInputPort();	// This is where we read the byte from the stream...

					id = id | (databyte & 0xFF);		// We append the byte on to ID...

					if (i != IdLength-1)				// If this is not the last byte, then slide the
					{									// previously appended byte to the left by one byte
						id = id << 8;					// to make room for the next byte we append to the ID

					} // if

					bytesread++;						// Increment the byte count

				} // for

				/*************************************************************
				 *	Classify the measurement
				 **************************************************************/

				measurement = 0;

				for (i=0; i<MeasurementLength; i++ )
				{
					databyte = ReadFilterInputPort();
					measurement = measurement | (databyte & 0xFF);	// We append the byte on to measurement...

					if (i != MeasurementLength-1)					// If this is not the last byte, then slide the
					{												// previously appended byte to the left by one byte
						measurement = measurement << 8;				// to make room for the next byte we append to the
																	// measurement
					} // if

					bytesread++;									// Increment the byte count

				} // if

				if ( id == Measurement.TIME.getID() )
				{
					//CHANGE: Instead of system.out, this filter passes time measurement
					
					writeMeasurementToFilterOutputPort(Measurement.TIME, measurement);
					
				} // if
				
				else if ( id == Measurement.ALTITUDE.getID() )
				{
					//CHANGE: this section is an addition, converting the altitude into meters and passes value
					
					//convert to meters
					double altMeters = Double.longBitsToDouble(measurement) * 0.3048;
					
					writeMeasurementToFilterOutputPort(Measurement.ALTITUDE, altMeters);
					
				} // if

				else if ( id == Measurement.PRESSURE.getID() )
				{
					//CHANGE: this section is an addition, passing the pressure value 
					
					writeMeasurementToFilterOutputPort(Measurement.PRESSURE, Double.longBitsToDouble(measurement));
				
				} // if

				else if ( id == Measurement.TEMPERATURE.getID() )
				{
					//CHANGE: converts Fahrenheit to Celsius and passes value
					
					//convert to celsius
					double celsius = (Double.longBitsToDouble(measurement)-32.0)*5.0/9.0;
					
					writeMeasurementToFilterOutputPort(Measurement.TEMPERATURE, celsius);

				} // if

			} // try

			/*******************************************************************************
			 *	Close down upon EndOfStreamException
			 ********************************************************************************/

			catch (EndOfStreamException e)
			{
				ClosePorts();
				System.out.print( "\n" + this.getName() + "::Converter Exiting; bytes read: " + bytesread +"\n");
				break;

			} // catch

		} // while

   } // run

} // Converter